package trastienda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Armado;
import trastienda.modelo.Servicio;
import trastienda.util.ConexionBD;

public class ArmadoDAO extends BaseDAO{
	
	public Armado insertar(Armado ins) throws DAOExcepcion {
		
		String query = "INSERT INTO Armado(precioArmado, idSala) VALUES (?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setDouble(1, ins.getprecioArmado());
			stmt.setInt(2, ins.getidSala());
		
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
	
			
			// Obtener el ultimo id
			int idArmado = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				idArmado = rs.getInt(1);
			}		
			
		
			Collection<Servicio> servicios = ins.getServicios();
			
			for (Servicio sss : servicios) {
				

				query = "INSERT INTO Armado_has_Servicios(idArmado, idServicios) VALUES (?,?)";
				
				
				stmt = con.prepareStatement(query);
				stmt.setDouble(1, idArmado);
				stmt.setInt(2, sss.getIdServicios());
			
				i = stmt.executeUpdate();
				if (i != 1) {
					throw new SQLException("No se pudo insertar");
				}
			
			}
						
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}
	public Armado obtener(String nomSala) throws DAOExcepcion {
		Armado ins = new Armado();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select nomSala, caracteristicas, capMax from Armado where nomSala=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, nomSala);
			rs = stmt.executeQuery();
			if (rs.next()) {
				ins.setidArmado(rs.getInt(1));
				ins.setprecioArmado(rs.getDouble(2));
				ins.setidSala(rs.getInt(3));
								
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ins;
	}
	
	public Collection<Armado> listar() throws DAOExcepcion {
		Collection<Armado>c=new ArrayList<Armado>();
		Connection con=null;
		PreparedStatement stmt=null;
		ResultSet rs=null;
	 try{
		 con=ConexionBD.obtenerConexion();
		 String query="select nomSala, capMax, caracteristicas from salas;";
		 stmt=con.prepareStatement(query);
		 rs=stmt.executeQuery();
		 while(rs.next()){
			 Armado vo =new Armado();
			 vo.setnomSala(rs.getString("nomSala"));
			 vo.setprecioArmado(rs.getInt("capMAx"));
			 vo.setcaracteristicas(rs.getString("caracteristicas"));
			 
			 c.add(vo);
		 }
		 
	 } catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

}


